
const sql = require('mysql')
//var args = require(process.argv)


console.log("pro args:", process.argv)


const db = sql.createPool({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'game1',
    // insecureAuth:true
})
db.getConnection(function (err, conn) {
    if (err) {
        console.error("mysql connect err:", err)
        return
    }
    console.log("mysql connect ok")
})
//console.log("create db:", db)

console.log("query user:")
db.query('select * from d_rooms', (err, data) => {
    if (err) {
        /* 
        错误:ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
        解决:改为native的密码 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
        如果 'root'@'localhost' 报错  那么就 'root'@'%'
        */
        console.error("query users err:", err)
        return
    }
    console.log("----- get data:")
    for (let i = 0; i < data.length && i < 30; i++) {
        //console.log(i, data[i].bonus_pool)
        console.log(i, data[i])
    }
    console.log("----- get data end")
})

if (false) {
    db.query(
        'INSERT INTO d_shops (`id`, `uid`, `shop_id`, `num`, `created_at`, `updated_at`) VALUES (0, ?, ?, ?, ?, ?)',
        [11, 2222, 3, '2023-11-08 16:28:48.134', '2023-11-08 16:28:48.134'],
        (err, data) => {
            if (err) {
                console.error("exec d_shops err:", err)
                return
            }
            console.log("----- insert d_shops id:", data)
        })

}

if (false) {
    db.query(
        "INSERT INTO d_channels (`id`, `from_name`, `from_id`, `from_time`, `parent_id`, `secret_key`, `reg`, `create_time`, " +
        "`start_time`, `end_time`, `attr`) VALUES (0, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        ['初始渠道号-IOS', 111, 1, 0, 'jjuu67', 1, 1693897810, 1693843200, 1780561266, 0],
        (err, data) => {
            if (err) {
                console.error("exec d_channels err:", err)
                return
            }
            console.log("----- insert d_channels id:", data)
        })
}


db.query(
    "update d_channels set from_time = from_time + 1 where from_id = ?", [111], (err, data) => {
        if (err) {
            console.error("update d_channels err:", err)
            return
        }
        console.log("----- update d_channels id:", data)
    })


console.log("test mysql ok!!!")

